package com.nkzy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.EmpFromDTO;
import com.nkzy.domain.pojo.Emp;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.vo.EmpVO;
import com.nkzy.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emps")
@RestController
@CrossOrigin
public class EmpController {
    @Autowired
    private EmpService empService;
    //增加员工信息
    @PostMapping
    @Operation(summary = "增加员工信息")
    public Result saveEmp(@RequestBody EmpFromDTO empFromDTO) {
        EmpVO empVO = BeanUtil.copyProperties(empFromDTO, EmpVO.class);
        return Result.success(empVO);
    }
    //删除员工信息
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除员工信息")
    public Result deleteEmpById(@PathVariable Integer id) {
        empService.removeById(id);
        return Result.success(null);
    }
    //修改员工信息
    @PutMapping
    @Operation(summary = "修改员工信息")
    public Result updateEmp(@RequestBody EmpFromDTO empFromDTO) {
        Emp emp = BeanUtil.copyProperties(empFromDTO, Emp.class);
        empService.updateById(emp);
        return Result.success(null);
    }
    //查询员工信息
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询员工信息")
    public Result getEmpById(@PathVariable Integer id) {
        Emp emp = empService.getById(id);
        EmpVO empVO = BeanUtil.copyProperties(emp, EmpVO.class);
        return Result.success(empVO);
    }
    //查询所有员工信息
    @GetMapping("/all")
    @Operation(summary = "查询所有员工信息")
    public Result getAllEmp() {
        List<Emp> emps = empService.list();
        List<EmpVO> empVOS = BeanUtil.copyToList(emps, EmpVO.class);
        return Result.success(empVOS);
    }
}
